﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;


public partial class Bidder_BidAccProduct : System.Web.UI.Page
{
    string BS,BPNUM;
    string _strTable = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        Int32 PJID;
        Int64 AccountId_TBR;
        string sql;
        PJID = SysFun.ToInt(Request.QueryString["ProjectId"]);
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumCompany);
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {

            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            if (_Account != null)
            {
                AccountId_TBR = SysFun.ToLong(_Account.AccountId);
                Db db = new Db();
                System.Data.IDataReader dr;

                //判断是否能投标之投标人没有投标权限
                sql = "select min(BiddingSwitching) as BiddingSwitching,count(*) as cun From ProjectCompanyAttribution where accountid=" + AccountId_TBR.ToString() + " and projectid=" + PJID.ToString();
                dr = db.DataReader(sql);
                while (dr.Read())
                {
                    BPNUM = SysFun.ToTrim(dr["cun"].ToString());
                    BS = dr["BiddingSwitching"].ToString();
                }
                dr.Close();
                db.Close();
                if (BPNUM == "0" || BPNUM == "")
                {
                    Response.Write("<script>window.alert('您的企业暂时还没有被列为投标人，暂时不能投标，如有疑问请于招标中心联系！');</script>");
                    Response.Write("<script>window.location=\"Nothing.aspx?Projectid=" + PJID.ToString() + "\"</script>");

                }
                else
                {
                    showGridView();
                }
            }
        }
    }

    public void showGridView()
    {
        string strConditions;
        strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = _strTable;
        dbPage.Key = "Product.ProductId";
        dbPage.Where = strConditions;                        //
        //dbPage.SelectField = "bid_project_product.ProjectId,Product.Productid,ProductCode,ProductName,Medicinemodel,Outlookc,Unit,b.tb";//第一版
        dbPage.SelectField = "bid_project_product.ProjectId,Product.Productid,ProductCode,ProductName,Medicinemodel,Outlookc,Unit,b.tb";
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 20;//Request.QueryString["PS"]);
        dbPage.OrderBy = "ProductName,Outlookc,Medicinemodel";
        System.Data.IDataReader dr = dbPage.ExecuteReader();
        GridView1.DataSource = dr;
        GridView1.DataBind();
        dr.Close();
        dr.Dispose();
        db.Close();
        dbPage.Db.Close();
        db.Close();
        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键


    }
    public string fctConditions()
    {
        string _strWhere = "";//所有投标人确认投标的 status=1  生产企业参与投标的商品 ifbid<>0  or ifbid is null
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        if (_Account != null)
        {
            int _intProjectId = SysFun.ToInt(SysFun.IsSQL(Request.QueryString["ProjectId"]));

            //if (_Account.Sort == "0")//判断登录的人是否为生产企业
            //{
            //    //    //增加投标人第一版
            //    _strTable = " Product INNER JOIN bid_project_product ON Product.ProductId = bid_project_product.ProductId inner join (Select Distinct ProductId,1 as tb From Project_Bid,Goods Where  (goods.ifbid<>0 or goods.ifbid is null) and goods.checkstatus=1  and Projectid='" + _intProjectId.ToString() + "' And Project_Bid.Goodsid=Goods.GoodsId And Project_Bid.Accountid=" + _Account.AccountId.ToString() + " ) AS b on Product.Productid=b.ProductId  INNER JOIN bid_goods_tbr on Product.Productname=bid_goods_tbr.Product_name ";
            //    _strWhere = " Product.ProductId in (select ProductId from Goods where AccountId_SC=" + _Account.AccountId + ") and bid_project_product.Projectid=" + _intProjectId.ToString() + " and bid_goods_tbr.Accountid_TB=" + _Account.AccountId + " and";




            //}
            //else
            //{
            //    _strTable = " Product INNER JOIN bid_project_product ON Product.ProductId = bid_project_product.ProductId inner join (Select Distinct ProductId,1 as tb From Project_Bid,Goods Where (goods.ifbid<>0 or goods.ifbid is null) and goods.checkstatus=1 and Projectid='" + _intProjectId.ToString() + "' And Project_Bid.Goodsid=Goods.GoodsId And Project_Bid.Accountid=" + _Account.AccountId.ToString() + ") AS b on Product.Productid=b.ProductId INNER JOIN bid_goods_tbr on Product.Productname=bid_goods_tbr.Product_name ";
            //    _strWhere += " bid_project_product.Projectid=" + _intProjectId.ToString() + " and Product.productid in (select productid  from goods where checkstatus=1 and ( ifbid <>0 or ifbid is null ) and accountid_sc in (select accountid_sc from bid_goods_tbr where accountid_tb=" + _Account.AccountId.ToString() + " and bid_goods_tbr.projectid= " + _intProjectId.ToString() + ")) and";

            //}
            //add by ws for 投标人设置第二版
            _strTable = " Product INNER JOIN bid_project_product ON Product.ProductId = bid_project_product.ProductId  left join (Select Distinct ProductId,1 as tb From Project_Bid,Goods Where (goods.ifbid<>0 or goods.ifbid is null) and goods.checkstatus=1 and Projectid='" + _intProjectId.ToString() + "' And Project_Bid.Goodsid=Goods.GoodsId And Project_Bid.Accountid=" + _Account.AccountId.ToString() + ") AS b on Product.Productid=b.ProductId  ";
            _strWhere += " bid_project_product.Projectid=" + _intProjectId.ToString() + " and Product.productid in (select distinct productid  from goods inner join bid_goods_tbr on goods.goodsid=bid_goods_tbr.goodsid where bid_goods_tbr.status=1 and accountid_tb=" + _Account.AccountId.ToString() + " and bid_goods_tbr.projectid= " + _intProjectId.ToString() + ") and";

            //改为inner join 效率感觉比上面的还要低

            //_strTable = " Product INNER JOIN bid_project_product ON Product.ProductId = bid_project_product.ProductId  inner join (Select Distinct ProductId,1 as tb From Project_Bid,Goods Where (goods.ifbid<>0 or goods.ifbid is null) and goods.checkstatus=1 and Projectid='" + _intProjectId.ToString() + "' And Project_Bid.Goodsid=Goods.GoodsId And Project_Bid.Accountid=" + _Account.AccountId.ToString() + ") AS b on Product.Productid=b.ProductId INNER JOIN goods on product.productid=goods.productid INNER JOIN bid_goods_tbr on Product.Productname=bid_goods_tbr.Product_name and goods.accountid_sc=bid_goods_tbr.accountid_sc and bid_project_product.Projectid=bid_goods_tbr.Projectid";
            //_strWhere += " bid_project_product.Projectid=" + _intProjectId.ToString() + " and  (goods.ifbid<>0 or goods.ifbid is null) and goods.checkstatus=1  and bid_goods_tbr.accountid_tb=" + _Account.AccountId.ToString() +" and";

            string _strProductCode = txtProductCode.Text;
            if (_strProductCode != "")
            {
                _strWhere += " ProductCode like '%" + _strProductCode + "%' And";
            }
            string _strProductName = txtProductName.Text;
            if (_strProductName != "")
            {
                _strWhere += " ProductName like '%" + _strProductName + "%' And";
            }
            string _strMedicinemodel = txtMedicinemodel.Text;
            if (_strMedicinemodel != "")
            {
                _strWhere += " Medicinemodel like '%" + _strMedicinemodel + "%' And";
            }
            string _strOutlookc = txtOutlookc.Text;
            if (_strOutlookc != "")
            {
                _strWhere += " Outlookc like '%" + _strOutlookc + "%' And";
            }
            if (_strWhere != "")
            {
                _strWhere = "  " + _strWhere.Substring(0, _strWhere.Length - 3);
            }

        }
        return _strWhere;

    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int tb =SysFun.ToInt (DataBinder.Eval(e.Row.DataItem, "tb"));
            if (tb == 1)
            {
                e.Row.BackColor = System.Drawing.Color.Gold;
            }
        }
        if ((BS == "0") || (BPNUM == "0"))
        {
            e.Row.Cells[5].Visible = false;
        }
        //e.Row.Cells[0].Visible = false;
    }
}
